package com.anjie.powerproject.controller;

import com.alibaba.fastjson.JSON;
import com.anjie.powerproject.common.Result;
import com.anjie.powerproject.entity.GraphInfo;
import com.anjie.powerproject.pojo.dto.CompareQueryDTO;
import com.anjie.powerproject.pojo.dto.StrategyCompareDTO;
import com.anjie.powerproject.pojo.dto.StrategyCompareQueryDTO;
import com.anjie.powerproject.service.GraphInfoService;
import com.anjie.powerproject.service.StrategyDisplayService;
import com.anjie.powerproject.strategy.BaseWorkStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping(value = "model/strategy")
public class StrategyDisplayController {
    private static final Logger logger = LoggerFactory.getLogger(StrategyDisplayController.class);

    @Autowired
    StrategyDisplayService strategyDisplayService;
    @Autowired
    BaseWorkStrategy baseWorkStrategy;
    @Autowired
    GraphInfoService graphInfoService;

    @RequestMapping(value = "compare")
    public Result<StrategyCompareDTO> compare(@RequestBody StrategyCompareQueryDTO queryDTO) {
        try {
            Date now = new Date();
            Integer graphId = baseWorkStrategy.getGraphIdByDate(now);
            GraphInfo graphInfo=graphInfoService.findGraphInfoById(graphId);
            if(queryDTO.getTargetModelStrategyType()==null){
                if(graphInfo.getStrategyType()==null)
                    queryDTO.setTargetModelStrategyType("1001");
                else
                    queryDTO.setTargetModelStrategyType(String.valueOf(graphInfo.getStrategyType()));
            }
            if(queryDTO.getSourceModelStrategyType()==null){
                if(graphInfo.getStrategyType()==null)
                    queryDTO.setSourceModelStrategyType("1001");
                else
                    queryDTO.setSourceModelStrategyType(String.valueOf(graphInfo.getStrategyType()));
            }
            return Result.ok(strategyDisplayService.compare(queryDTO));
        } catch (Exception ex) {
            logger.error("获取能量模型比较数据失败 params={} errmsg={}", JSON.toJSONString(queryDTO), ex.getMessage(), ex);
            return Result.error(String.format("获取能量模型比较数据失败 [%s]", ex.getMessage()));
        }
    }
}
